package com.karolak.web.saf.command.scoped;

import com.karolak.service.Service;
import com.karolak.web.saf.action.ModelAction;
import com.karolak.web.saf.command.UpdateCommand;
import com.opensymphony.xwork2.Action;

public class ScopedUpdateCommand extends UpdateCommand {

	@SuppressWarnings("unchecked")
	@Override
	public <T, Id, S extends Service<T, Id>> void prepare(ModelAction<T, Id, S> action) {
		T entity = (T) action.getSession().get(action.getScopeKey());
		action.setModel(entity);
		if (log.isTraceEnabled()) {
			log.trace("Entity to be updated:" + action.getModel());
		}
	}

	@Override
	public <T, Id, S extends Service<T, Id>> String execute(ModelAction<T, Id, S> action) {
		String result = super.execute(action);
		if (Action.SUCCESS.equals(result)) {
			action.getSession().remove(action.getScopeKey());
		}
		return result;
	}
}
